/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：GradeRuleDaoImpl.java
 * History:
 *       May 11, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import net.sf.json.JSONObject;

import com.xteam.ecsystem.dao.IGradeRuleDao;
import com.xteam.ecsystem.entities.BaseGradeRule;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 等级规则数据访问实现类
 * 
 * @author liuzhi
 *
 */
@Repository
public class GradeRuleDaoImpl extends DDLPrefix implements IGradeRuleDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	@SuppressWarnings("unchecked")
	public String getGradeRuleList(int pageIndex, int pageSize) {
		JSONObject jo = new JSONObject();
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BaseGradeRule.REF + SQL_WHERE + "1=1";
			String countHQL = SQL_SELECT + "count(*)" + SQL_FROM + BaseGradeRule.REF + SQL_WHERE + "1=1";
			
			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao.queryList(countHQL, hql, list
					.toArray(), pageIndex * pageSize, pageSize);
			
			jo = JSONObject.fromObject(p);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public void saveOrUpdateObject(Object obj, boolean isAddNew) {
		try {
			BaseGradeRule bgr = (BaseGradeRule)obj;
			
			if (isAddNew) {
				String sql = "insert into base_grade_rule values(?, ?, ?, ?, ?, ?, ?, ?)";
				this.baseDao.executeUpdateWithSQL(sql, new Object[]{
					bgr.getBgrId(),
					bgr.getBgrName(),
					bgr.getBgrUprule(),
					bgr.getBgrUprulesql(),
					bgr.getBgrDownrule(),
					bgr.getBgrDownrulesql(),
					bgr.getBgrIcon(),
					bgr.getBgrState()
				});
			}else{
				String hql = "update " + BaseGradeRule.REF + " set bgrName=?,bgrUprule=?,bgrUprulesql=?,bgrDownrule=?,bgrDownrulesql=?,bgrIcon=?,bgrState=? where bgrId=?";
				this.baseDao.executeUpdate(hql, new Object[]{
					bgr.getBgrName(),
					bgr.getBgrUprule(),
					bgr.getBgrUprulesql(),
					bgr.getBgrDownrule(),
					bgr.getBgrDownrulesql(),
					bgr.getBgrIcon(),
					bgr.getBgrState(),
					bgr.getBgrId()
				});
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getGradeRuleById(String id) {
		JSONObject jo = new JSONObject();
		
		try {
			String hql = SQL_FROM + BaseGradeRule.REF + SQL_WHERE + "bgrId=?";
			
			jo = JSONObject.fromObject((BaseGradeRule)this.baseDao.findObject(hql, new Object[]{Integer.parseInt(id)}));

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public void updateGradeRuleStateById(String id, int state) {
		try {
			String hql = "update " + BaseGradeRule.REF + " set bgrState=? where bgrId=?";
			
			this.baseDao.executeUpdate(hql, new Object[]{state, Integer.parseInt(id)});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public String getBgrTopData(){
		JSONObject jo = new JSONObject();
		List list = new ArrayList();
		
		try {
			String hql = SQL_FROM + BaseGradeRule.REF + SQL_ORDERBY + "bgrId" + SQL_DESC;
			list = this.baseDao.find(hql);
			
			jo = JSONObject.fromObject((BaseGradeRule)list.get(0));

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}
	
}
